package com.ruoyi.cases.service.impl;

import java.util.List;

import com.ruoyi.cases.mapper.JlArchivesTypeMapper;
import com.ruoyi.common.core.domain.entity.JlArchivesType;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysRoleMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cases.mapper.JlArchivesMapper;
import com.ruoyi.cases.domain.JlArchives;
import com.ruoyi.cases.service.IJlArchivesService;

/**
 * 档案管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-20
 */
@Service
public class JlArchivesServiceImpl implements IJlArchivesService 
{
    @Autowired
    private JlArchivesMapper jlArchivesMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
@Autowired
private JlArchivesTypeMapper archivesTypeMapper;
    /**
     * 查询档案管理
     * 
     * @param id 档案管理主键
     * @return 档案管理
     */
    @Override
    public JlArchives selectJlArchivesById(Long id)
    {
        return jlArchivesMapper.selectJlArchivesById(id);
    }

    /**
     * 查询档案管理列表
     * 
     * @param jlArchives 档案管理
     * @return 档案管理
     */
    @Override
    public List<JlArchives> selectJlArchivesList(JlArchives jlArchives)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String[] deptTypes = sysRoleMapper.getDeptTypes(loginUser.getDeptId());
        List<JlArchivesType> jlArchivesTypes = archivesTypeMapper.selectJlArchivesTypeList(new JlArchivesType());
        Long[] typeIds = jlArchivesTypes.stream().filter(j -> ArrayUtils.contains(deptTypes, j.getTypeNo().substring(0, 1))).map(s -> s.getId()).toArray(Long[]::new);
        jlArchives.setTypeIds(typeIds);
        return jlArchivesMapper.selectJlArchivesList(jlArchives);
    }

    /**
     * 新增档案管理
     * 
     * @param jlArchives 档案管理
     * @return 结果
     */
    @Override
    public int insertJlArchives(JlArchives jlArchives)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        jlArchives.setCreateTime(DateUtils.getNowDate());
        jlArchives.setCreateBy(loginUser.getUsername());
        jlArchives.setDeptId(loginUser.getDeptId());
        return jlArchivesMapper.insertJlArchives(jlArchives);
    }

    /**
     * 修改档案管理
     * 
     * @param jlArchives 档案管理
     * @return 结果
     */
    @Override
    public int updateJlArchives(JlArchives jlArchives)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        jlArchives.setUpdateTime(DateUtils.getNowDate());
        jlArchives.setUpdateBy(loginUser.getUsername());
        return jlArchivesMapper.updateJlArchives(jlArchives);
    }

    /**
     * 批量删除档案管理
     * 
     * @param ids 需要删除的档案管理主键
     * @return 结果
     */
    @Override
    public int deleteJlArchivesByIds(Long[] ids)
    {
        return jlArchivesMapper.deleteJlArchivesByIds(ids);
    }

    /**
     * 删除档案管理信息
     * 
     * @param id 档案管理主键
     * @return 结果
     */
    @Override
    public int deleteJlArchivesById(Long id)
    {
        return jlArchivesMapper.deleteJlArchivesById(id);
    }
}
